//
//  SQLiteTableiabe+HandyJSON.swift
//  MYModule
//
//  Created by HFY on 2022/11/7.
//

import UIKit
import SQLite
import HandyJSON
import SwiftDate
import MYModule

public extension SQLiteTableiabe where Self: HandyJSON {
    
    
    
       
    static func deserialize(from id: Int64) ->Self? {
        
        let row = try? db.pluck(table.where(expression_id == id))
        return deserialize(from: row)
        
    }
    
   
    static func deserialize(from row: Row?) ->Self? {
        
        guard let row = row else {
            return nil
        }
        
        let dict = dict(row: row)

        let model = deserialize(from: dict)

        return model
    }
    
    
    
}

public extension Array where Element: (HandyJSON & SQLiteTableiabe) {
    
    static func deserialize(from anySequence: AnySequence<Row>?) -> [Element] {
        
        guard let anySequence = anySequence else {
            return []
        }
 
        var models: [Element] = []
        for sequence in anySequence {
            if let model = Element.deserialize(from: sequence) {
                models.append(model)
            }
        }
      
        return models
    }
    
    static func deserialize(from statement: Statement?) ->[Element]? {
        
        guard let statement = statement else {
            return []
        }
        
        let dictArray = Element.dictArray(statement: statement)
        let models = [Element].deserialize(from: dictArray)?.compactMap({ $0 })
        return models
    }
    
   
}







